myApp.onPageInit('meeting-apply', function (page) {
    console.log("onPageInit " + page.name);
    var meetingData;

    var list = $$(page.container).find('.list-block ul');
    $$.getJSONP(SERVER_PATH + 'renderMeetingApply.do', function (json) {
        if (json.code == 1) {
            meetingData = json.results;

            buildComponents(list, meetingData.columns,null);

            list.append('<li class="item-divider">&nbsp;</li>');

            // 当前部门
            if (meetingData.process.currentDeparts){
                var li = $$('<li><div class="item-content"><div class="item-inner item-inner-long">' +
                        '<div class="item-title label">当前部门</div>' +
                        '<div class="item-input radio-group"></div>' +
                        '</div></div></li>'),
                    radioGroup = li.find('.item-input');
                $$.each(meetingData.process.currentDeparts, function (index, value) {
                    radioGroup.append('<label class="label-radio">'
                        + '<input type="radio" name="departId" value="' + value.departId + (index === 0 ? '" checked="checked">' : '">')
                        + '<div class="form-checkbox">' + value.departName + '</div>'
                        + '</label>');
                });
                list.append(li);
            }

            // 下一步办理步骤
            {
                var li = $$('<li><div class="item-content"><div class="item-inner item-inner-long">' +
                        '<div class="item-title label">选择下一步办理步骤</div>' +
                        '<div class="item-input radio-group"></div>' +
                        '</div></div></li>'),
                    radioGroup = li.find('.item-input');
                $$.each(meetingData.process.nextNodeList, function (index, value) {
                    radioGroup.append('<label class="label-radio">'
                        + '<input type="radio" name="outcome" value="' + value.nodeLineName + (index === 0 ? '" checked="checked">' : '">')
                        + '<div class="form-checkbox">' + value.nodeLineName + '</div>'
                        + '</label>');
                });
                list.append(li);
            }

            // 下一步处理人
            list.append('<li><div class="item-content"><div class="item-inner item-inner-long">' +
                '<div class="item-title label">选择下一步处理人</div>' +
                '<div class="item-input task-handle-user"></div>' +
                '</div></div></li>');

            list.append('<li><div class="item-content"><div class="item-inner row button-row"><a href="#" class="button active col-100">提交</a></div></div></li>');

            var handleUserInput = list.find('.task-handle-user');
            buildHandleUserInput2(handleUserInput, meetingData.process, 'selecTHandleUseIdHid');
            // change triggered on the checked element
            //list.find('input[name="outcome"]').change(function() {
            //    var nodeLineName = $$(this).val();
            //    console.log('select ' + nodeLineName);
            //    $$.getJSONP(SERVER_PATH + 'getNextFromStart.do', {
            //        formDefId: workData.formid,
            //        workflowdefinitionpdid: workData.workflowdefinitionpdid,
            //        nodeLineName: nodeLineName
            //    }, function (json) {
            //        if (json.code == 1) {
            //            buildHandleUserInput2(handleUserInput, json.results, 'selecTHandleUseIdHid');
            //        }
            //    });
            //});

            list.find('textarea.resizable').each(function () {
                myApp.resizableTextarea(this);
            }).change();
        }
    });

    list.on('click', '.button.active', function () {
        var data = myApp.formToJSON(list);
        $$.each(data, function (key, value) {
            if ($$.isArray(value)) {
                data[key] = value.join(',');
            }
        });

        // validate
        for (var i = 0; i < meetingData.columns.length; i++) {
            var comp = meetingData.columns[i];
            if (comp.required && comp.type === 'edit' && !data[comp.columnName]) {
                myApp.toast(comp.displayName + '未填写', '<div>×</div>', {}).show(true);
                return;
            }

            if (comp.columnType == 'datetime') {
                data[comp.columnName] = data[comp.columnName].replace('T', ' ');
            }
        }

        myApp.showIndicator();
        $$.getJSONP(SERVER_PATH + 'startMeetingProcess.do?process=true', data, function (json) {
            myApp.hideIndicator();
            if (json.code == 1) {
                myApp.toast('成功', '<div>√</div>', {}).show(true);
                page.view.back();
            } else {
                myApp.toast(json.msg, '<div>×</div>', {}).show(true);
            }
        });
    });
});


myApp.onPageInit('selectRoom', function (page) {
    console.log("onPageInit " + page.name);
    var params = page.query,
        delegateName = page.query.delegate,
        delegate = window[delegateName];
    window[delegateName] = undefined;
    var ul = $$(page.container).find('.list-block ul');

    params.beginTime = decodeURIComponent(params.beginTime);
    params.endTime = decodeURIComponent(params.endTime);
    $$.getJSONP(SERVER_PATH + 'selectRoom.do', {beginTime: params.beginTime, endTime: params.endTime}, function (json) {
        if (json.code == 1) {
            $$.each(json.results.rooms, function (index, value) {
                ul.append('<li><label class="label-radio item-content">' +
                    '<input type="radio" name="roomId" value="' + value.roomId + (delegate.selectedRoom == value.roomId ? '" checked="checked"' : '"') +
                        (value.isOccupancy ? ' disabled>' : '>') +
                    '<div class="item-inner"' + (value.isOccupancy ? ' style="opacity: 0.55"' : '') + '>' +
                    '<div class="item-title">' + value.roomName + '</div>' +
                    '</div>' +
                    '</label></li>');

                ul.find('input[name="roomId"]').change(function () {
                    var roomId = $$(this).val();
                    var roomName = $$(this).parent().find('.item-title').text();
                    delegate.delegate({roomId: roomId, roomName: roomName});
                    page.view.router.back();
                });
            });
        }
    });
});
